package com.wootide.cms.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class JspFileUpload
{
    /*  29 */private HttpServletRequest request = null;

    /*  32 */private String uploadPath = null;

    /*  35 */private static int BUFSIZE = 8192;

    /*  38 */private Hashtable paramHt = new Hashtable();

    /*  41 */private ArrayList updFileArr = new ArrayList();

    public void setRequest(HttpServletRequest request)
    {
        /*  50 */this.request = request;
    }

    public void setUploadPath(String path)
    {
        /*  60 */this.uploadPath = path;
    }

    public int process()
    {
        /*  71 */int status = 0;

        /*  74 */status = preCheck();

        /*  76 */if (status != 0)
        {
            /*  77 */return status;
        }
        try
        {
            /*  81 */String name = null;

            /*  83 */String value = null;

            /*  85 */boolean fileFlag = false;

            /*  87 */File tmpFile = null;

            /*  89 */String fName = null;

            /*  91 */FileOutputStream baos = null;
            /*  92 */BufferedOutputStream bos = null;

            /*  94 */this.paramHt = new Hashtable();
            /*  95 */this.updFileArr = new ArrayList();

            /*  97 */int rtnPos = 0;
            /*  98 */byte[] buffs = new byte[BUFSIZE * 8];

            /* 100 */String contentType = this.request.getContentType();
            /* 101 */int index = contentType.indexOf("boundary=");
            /* 102 */String boundary = "--" + contentType.substring(index + 9);
            /* 103 */String endBoundary = boundary + "--";

            /* 105 */ServletInputStream sis = this.request.getInputStream();

            /* 107 */while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1)
            {
                /* 108 */String strBuff = new String(buffs, 0, rtnPos);

                /* 110 */if (strBuff.startsWith(boundary))
                {
                    /* 111 */if ((name != null) && (name.trim().length() > 0))
                    {
                        /* 113 */if (fileFlag)
                        {
                            /* 114 */bos.flush();
                            /* 115 */baos.close();
                            /* 116 */bos.close();
                            /* 117 */baos = null;
                            /* 118 */bos = null;
                            /* 119 */this.updFileArr.add(fName);
                        }
                        else
                        {
                            /* 121 */Object obj = this.paramHt.get(name);
                            /* 122 */ArrayList al = new ArrayList();
                            /* 123 */if (obj != null)
                            {
                                /* 124 */al = (ArrayList)obj;
                            }
                            /* 126 */al.add(value);

                            /* 128 */this.paramHt.put(name, al);
                        }
                    }
                    /* 131 */name = new String();
                    /* 132 */value = new String();
                    /* 133 */fileFlag = false;
                    /* 134 */fName = new String();
                    /* 135 */rtnPos = sis.readLine(buffs, 0, buffs.length);
                    /* 136 */if (rtnPos != -1)
                    {
                        /* 137 */strBuff = new String(buffs, 0, rtnPos);
                        /* 138 */if (strBuff.toLowerCase().startsWith(
                        /* 139 */"content-disposition: form-data; "))
                        {
                            /* 140 */int nIndex =
                                strBuff.toLowerCase().indexOf(
                                /* 141 */"name=\"");
                            /* 142 */int nLastIndex =
                                strBuff.toLowerCase().indexOf(
                                /* 143 */"\"", nIndex + 6);
                            /* 144 */name =
                                strBuff.substring(nIndex + 6, nLastIndex);
                        }
                        /* 146 */int fIndex = strBuff.toLowerCase().indexOf(
                        /* 147 */"filename=\"");
                        /* 148 */if (fIndex != -1)
                        {
                            /* 149 */fileFlag = true;
                            /* 150 */int fLastIndex =
                                strBuff.toLowerCase().indexOf(
                                /* 151 */"\"", fIndex + 10);
                            /* 152 */fName =
                                strBuff.substring(fIndex + 10, fLastIndex);
                            /* 153 */fName = getFileName(fName);
                            /* 154 */if ((fName == null)
                                || (fName.trim().length() == 0))
                            {
                                /* 155 */fileFlag = false;
                                /* 156 */sis.readLine(buffs, 0, buffs.length);
                                /* 157 */sis.readLine(buffs, 0, buffs.length);
                                /* 158 */sis.readLine(buffs, 0, buffs.length);
                            }
                            else
                            {
                                /* 161 */fName = getFileNameByTime(fName);
                                /* 162 */sis.readLine(buffs, 0, buffs.length);
                                /* 163 */sis.readLine(buffs, 0, buffs.length);
                            }
                        }
                    }
                    /* 167 */}
                else if (strBuff.startsWith(endBoundary))
                {
                    /* 168 */if ((name != null) && (name.trim().length() > 0))
                    {
                        /* 169 */if (fileFlag)
                        {
                            /* 170 */bos.flush();
                            /* 171 */baos.close();
                            /* 172 */bos.close();
                            /* 173 */baos = null;
                            /* 174 */bos = null;
                            /* 175 */this.updFileArr.add(fName);
                        }
                        else
                        {
                            /* 177 */Object obj = this.paramHt.get(name);
                            /* 178 */ArrayList al = new ArrayList();
                            /* 179 */if (obj != null)
                            {
                                /* 180 */al = (ArrayList)obj;
                            }
                            /* 182 */al.add(value);
                            /* 183 */this.paramHt.put(name, al);
                        }
                    }
                }
                /* 187 */else if (fileFlag)
                {
                    /* 188 */if ((baos == null) && (bos == null))
                    {
                        /* 189 */tmpFile = new File(this.uploadPath + fName);
                        /* 190 */baos = new FileOutputStream(tmpFile);
                        /* 191 */bos = new BufferedOutputStream(baos);
                    }
                    /* 193 */bos.write(buffs, 0, rtnPos);
                    /* 194 */baos.flush();
                }
                else
                {
                    /* 197 */value = value + strBuff;
                }
            }
        }
        catch (IOException e)
        {
            /* 203 */status = 4;
        }

        /* 206 */return status;
    }

    private int preCheck()
    {
        /* 210 */int errCode = 0;
        /* 211 */if (this.request == null)
            /* 212 */return 1;
        /* 213 */if ((this.uploadPath == null)
            || (this.uploadPath.trim().length() == 0))
        {
            /* 214 */return 2;
        }
        /* 216 */File tmpF = new File(this.uploadPath);
        /* 217 */if (!tmpF.exists())
        {
            /* 218 */return 2;
        }
        /* 220 */String contentType = this.request.getContentType();
        /* 221 */if (contentType.indexOf("multipart/form-data") == -1)
            /* 222 */return 3;
        /* 223 */return errCode;
    }

    public String getParameter(String name)
    {
        /* 228 */String value = "";
        /* 229 */if ((name == null) || (name.trim().length() == 0))
            /* 230 */return value;
        /* 231 */value =
            this.paramHt.get(name) == null ? ""
                : (String)((ArrayList)this.paramHt.get(name)).get(0);
        /* 232 */return value;
    }

    public String[] getParameters(String name)
    {
        /* 237 */if ((name == null) || (name.trim().length() == 0))
            /* 238 */return null;
        /* 239 */if (this.paramHt.get(name) == null)
            /* 240 */return null;
        /* 241 */ArrayList al = (ArrayList)this.paramHt.get(name);
        /* 242 */String[] strArr = new String[al.size()];
        /* 243 */for (int i = 0; i < al.size(); i++)
            /* 244 */strArr[i] = ((String)al.get(i));
        /* 245 */return strArr;
    }

    public int getUpdFileSize()
    {
        /* 249 */return this.updFileArr.size();
    }

    public String[] getUpdFileNames()
    {
        /* 253 */String[] strArr = new String[this.updFileArr.size()];
        /* 254 */for (int i = 0; i < this.updFileArr.size(); i++)
            /* 255 */strArr[i] = ((String)this.updFileArr.get(i));
        /* 256 */return strArr;
    }

    private String getFileName(String input)
    {
        /* 259 */int fIndex = input.lastIndexOf("\\");
        /* 260 */if (fIndex == -1)
        {
            /* 261 */fIndex = input.lastIndexOf("/");
            /* 262 */if (fIndex == -1)
            {
                /* 263 */return input;
            }
        }
        /* 266 */input = input.substring(fIndex + 1);
        /* 267 */return input;
    }

    private String getFileNameByTime(String input)
    {
        /* 271 */int index = input.indexOf(".");
        /* 272 */Date dt = new Date();
        /* 273 */SimpleDateFormat sdf =
            new SimpleDateFormat("yyyyMMddHHmmssSSS");

        /* 275 */return "files_" + sdf.format(dt) + input.substring(index);
    }
}
